{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca8bfad5-86f1-40f2-bfce-8807df4e2482",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy.stats as st\n",
    "import seaborn as sns\n",
    "sns.set_style('whitegrid')\n",
    "import matplotlib.pyplot as plt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a63232-9ae5-4882-9117-71950fc7926f",
   "metadata": {},
   "outputs": [],
   "source": [
    "stm = pd.read_csv(r\"D:\\dasanxia\\steam.csv\")\n",
    "#现在拿到所有用户对所有游戏的时长\n",
    "stm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29a4872f-d615-4254-919b-e6dea66590c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#这一步想去掉购买后又没玩的游戏\n",
    "#stm_play = stm.loc[stm['is_played'] == 'play']\n",
    "#stm_play\n",
    "stm.loc[stm['is_played']=='purchase','hours'] = 0\n",
    "stm#将买了的时间置0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3753753a-b1fc-4259-ba61-f260f645dffd",
   "metadata": {},
   "outputs": [],
   "source": [
    "alaytime1 = stm.iloc[:,[0,1,4]]\n",
    "a1 = alaytime1#分时长\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e184bfd-d1eb-43d1-8c74-92c9d0ac264b",
   "metadata": {},
   "outputs": [],
   "source": [
    "alaytime2=alaytime1['hours'].sum()\n",
    "a2 = alaytime2#总时长\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5cd9e47-22d5-4c56-b6ff-98ba3b1680f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "b1 = alaytime1['hours'].div(a2).mul(10000)\n",
    "b1#算出每个用户占所有时长的占比（为了计算扩大了10000倍）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc333a2b-40e9-4149-87b5-28d38f846e67",
   "metadata": {},
   "outputs": [],
   "source": [
    "#a1.insert(2,'occupy',b1)\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1633dc4-b1ac-4a8b-80cc-3d4345b3868f",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_ids = a1.user_id.unique()#获取所有用户id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea3c35b4-c0a8-436c-9638-3eaac9e2fd5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler(feature_range=(0,100))\n",
    "\n",
    "all_user_minmax = []\n",
    "for id in user_ids:\n",
    "    user_value = a1.loc[a1['user_id'] == id]\n",
    "    user_minmax = scaler.fit_transform(np.array(user_value['occupy']).reshape(-1,1))\n",
    "    all_user_minmax.append(user_minmax)\n",
    "all_user_minmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7257dde-96d9-4c24-b19c-7624e6f424ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_mean = []\n",
    "for val in all_user_minmax:\n",
    "    user_mean.append(val.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a036cc1-025c-4d96-99f0-8e95d05b69e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_index = []\n",
    "for id in user_ids:\n",
    "    user_index.append(a1[a1['user_id']==id]['game_id'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f04a41dc-c97e-49fb-a3c7-c4d97fbe4b8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "same = set(user_index[0]) & set(user_index[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc16981f-6e55-4204-bb35-5d7186b5077e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a1['occupy']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "818accc4-2c8f-4d8f-84db-f666794d88b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#分子\n",
    "fenzi = 0\n",
    "for k in same:\n",
    "    ruk = a1.loc[(a1['user_id']==3)&(a1['game_id']==k)]['occupy'].values\n",
    "    rvk = a1.loc[(a1['user_id']==0)&(a1['game_id']==k)]['occupy'].values\n",
    "    fenzi+=ruk*rvk\n",
    "    #print(ruk)\n",
    "print(fenzi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1590f4f1-2810-4087-86cf-4c167c1cbe8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#分母\n",
    "ruk1=0\n",
    "rvk1=0\n",
    "for k  in same:\n",
    "    ruk = a1.loc[(a1['user_id']==3)&(a1['game_id']==k)]['occupy'].values\n",
    "    ruk1 +=ruk*ruk\n",
    "ruk2= pow(ruk1,0.5)\n",
    "for k  in same:\n",
    "    rvk = a1.loc[(a1['user_id']==0)&(a1['game_id']==k)]['occupy'].values\n",
    "    rvk1+=rvk*rvk\n",
    "rvk2= pow(rvk1,0.5)   \n",
    "funmu= ruk2*rvk2\n",
    "print(funmu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ebc58ac-8c85-4286-a3cc-d19745e979a1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
